<?php

namespace App\Imports;

use App\Http\Util\Err;
use App\Http\Util\HomeTool;
use App\Http\Util\Tool;
use App\Models\AllExam;
use App\Models\Exam;
use App\Models\Question;
use App\Models\Room;
use App\Models\Sign;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;

class SignImport implements ToCollection
{

    public $cid;

    public function __construct($cid)
    {
        $this->cid = $cid;
    }

    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        try{
            // TODO: Implement collection() method.
            //解析excel中的数据
            $data=json_decode(json_encode($rows),true);
            //去掉第一行的标题
            array_splice($data, 0, 1);
            $log = '';
            $data = array_column($data,NULL,8);
            $data = array_values($data);
            $exams  =AllExam::where('id',$this->cid)->first()->toArray();
            //拼接字段入库
            foreach ($data as $row)
            {
                $row[12] = substr_replace($row[12], '', 0,2);
                $binary = pack("H*",$row[12]);
                $dir ="upload/csv_img/".date("Ymd");
                if (!is_dir($dir)){
                    mkdir($dir, 0777,true);
                }
                $uniqid = uniqid();
                file_put_contents(public_path($dir."/".$uniqid.".png"),$binary);
                $query['all_exam_id']=$this->cid??0;
//                if($row[3]){
//                    $row[3] = Tool::get_between($row[3],'[',']');
//                }
                $query['company_name'] = trim($row[0])??'';
                $query['third_pid'] = trim($row[7])??'';
                $query['id_card'] = trim($row[8])??'';
                $query['real_name']  = trim($row[9])??'';
                $query['card_type'] = 1;
                if($row[10] == '女'){
                    $query['sex'] = 2;
                }else{
                    $query['sex'] = 1;
                }
                $query['image'] ="/".$dir."/".$uniqid.".png";
                $check = Sign::where('id_card',$query['id_card'])
                            ->where('is_del',Err::DEL_SHOW)
                            ->where('all_exam_id',$this->cid)
                            ->first();
                if($check){
                   $check->update($query);
                }else{
                    $query = HomeTool::makeId($query,$exams);
                    if($exams['type'] == 'all' || $exams['type'] == 'exam'){
                        $room =  Exam::where('id',$exams['exam_id'])->value('room');
                        $room = json_decode($room);
                        $now_count = Sign::where('all_exam_id',$this->cid)
                            ->where('is_del',Err::DEL_SHOW)
                            ->count();
                        foreach($room as $k1 => $v1){
                            $room_number = Room::where('id',$v1)->value('numbers');
                            if($now_count < $room_number){
                                $query['room_id'] = $v1;
                                break;
                            }
                        }
                        if(!array_key_exists('room_id',$query)){
                            $query['room_id']  =  end($room);
                        }
                    }
                    if(!Sign::create($query)){
                        Tool::logInfo('数据导入失败', json_encode($query), $file = 'lumen');
                    }
                }
            }
        }catch (\Exception $e){
            echo $e->getMessage();
        }
    }


    public function createData($rows)
    {
        //todo
    }
}
